<?php

namespace backend\controllers;

use common\cache\DictCache;
use common\models\Address;
use common\models\Campus;
use common\models\Coupon;
use common\models\Order;
use common\models\User;
use common\models\UserCoupon;
use common\models\Water;
use Yii;

/**
 * 用户管理
 * Class UserController
 * @package backend\controllers
 */
class UserController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = User::className();
    }

    /**
     * 列表
     * @return array|string
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isPost) {
            $where = [];
            $data = parent::actionIndex(true, $where);
            $userids = array_column($data['data'],'second_level');
            $user = [];
            if ($userids){
                $user = (new User())->getUserHtml($userids);
            }
            foreach ($data['data'] as &$datum){
                $info = json_decode($datum['info'],true);
                $datum['nickname'] = $info['nickName'];
                $datum['avatar'] = $info['avatarUrl'];
                $datum['second_title'] = $user[$datum['second_level']]??'';
            }
            return $data;
        } else {
            return parent::actionIndex();
        }
    }

    /**
     * 详情
     * @return bool|string
     */
    public function actionDetail()
    {
        $id = Yii::$app->request->get('id','');
        $model = User::find()->where(['id'=>$id])->one();
        if (!$model){
            return $this->bError('暂无该用户');
        }
        $info = json_decode($model->info,true);
        $model->nickname = $info['nickName'];
        $model->headimgurl = $info['avatarUrl'];
        $userids = [];
        if ($model->one_level){
            $userids[] = $model->one_level;
        }
        if ($model->second_level){
            $userids[] = $model->second_level;
        }
        if ($model->top_level){
            $userids[] = $model->top_level;
        }
        $res = (new User())->getUserHtml($userids,0);
        return $this->render('',['model'=>$model,'id'=>$id,'res'=>$res]);
    }

    /**
     * 地址列表
     * @return array|string
     */
    public function actionAddress()
    {
        $this->model = Address::className();
        if (Yii::$app->request->isPost) {
            $userid = Yii::$app->request->get('userid','');
            $data = parent::actionIndex(true,['userid'=>$userid]);
            $campus_ids = array_column($data['data'],'campus_id');
            $campus = (new Campus())->getCampusName($campus_ids);
            foreach ($data['data'] as &$datum){
                $datum['campus'] = $campus[$datum['campus_id']]??'';
            }
            return $data;
        } else {
            return parent::actionIndex();
        }
    }

    /**
     * 我的订单
     * @return array|string
     */
    public function actionOrder()
    {
        $this->model = Order::className();
        if (Yii::$app->request->isPost) {
            $userid = Yii::$app->request->get('userid','');
            $data = parent::actionIndex(true,['userid'=>$userid]);
            $delivery_method = DictCache::get('delivery_method');
            $order_status = DictCache::get('order_status');
            foreach ($data['data'] as &$datum){
                $delivery_method_title = $delivery_method[$datum['delivery_method']]??'';
                $datum['order_status_title'] = $order_status[$datum['order_status']]??'';
                $addr = "<div>提货方式：".$delivery_method_title."</div>";
                $receipt_info = $datum['receipt_info']?json_decode($datum['receipt_info'],true):[];
                if ($datum['delivery_method']==2){
                    $addr .= "<div>校区：".$receipt_info['campus_name'].'<div>';
                    $addr .= "<div>收货人：".$receipt_info['consignee'].'<div>';
                    $addr .= "<div>联系方式：".$receipt_info['mobile'].'<div>';
                    $addr .= "<div>楼号/门牌号：".$receipt_info['address'].'<div>';
                }
                $datum['addr'] = $addr;
            }
            return $data;
        } else {
            return parent::actionIndex();
        }
    }

    /**
     * 资金流水
     * @return array|string
     */
    public function actionWater()
    {
        $this->model = Water::className();
        if (Yii::$app->request->isPost){
            $userid = Yii::$app->request->get('userid','');
            $data = parent::actionIndex(true,['userid'=>$userid]);
            $fund_type = DictCache::get('fund_type');
            foreach ($data['data'] as &$datum){
                $datum['fund_type'] = $fund_type[$datum['fund_type']]??'';
                if ($datum['add_sub']==1){
                    $datum['add_sub'] = '<span class="badge badge-primary">+</span>';
                }else{
                    $datum['add_sub'] = '<span class="badge badge-warning">-</span>';
                }
            }
            return $data;
        } else {
            return parent::actionIndex();
        }
    }

    /**
     * 我的卡券
     * @return array|string
     */
    public function actionCoupon()
    {
        $this->model = UserCoupon::className();
        if (Yii::$app->request->isPost){
            $userid = Yii::$app->request->get('userid','');
            $data = parent::actionIndex(true,['userid'=>$userid]);
            $coupon = new Coupon();
            $campus_ids = array_column($data['data'],'campus_id');
            $campus = (new Campus())->getCampusName($campus_ids);
            $arr = [];
            $user_coupon_use = DictCache::get('user_coupon_use');
            foreach ($data['data'] as &$datum){
                $coupon_info = json_decode($datum['coupon_info'],true);
                $one = $coupon->oneData(0,[],[],0,$campus,$coupon_info);
                $one['discount_amount'] = $datum['discount_amount'];
                $one['status_title'] = $user_coupon_use[$datum['use']]??'';
                $one['create_time'] = $datum['create_time'];
                $one['use_time'] = $datum['use_time'];
                $arr[] = $one;
            }
            $data['data'] = $arr;
            return $data;
        } else {
            return parent::actionIndex();
        }
    }
}